#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn=10, maxm=1<<(maxn-1), maxsize=1<<maxn;
int f[maxsize][maxm+1], u[maxsize][maxsize], v[maxsize][maxsize], c[maxsize];

void solve(int o, int dep, int size, int m) {
	for (int i=0; i<=m && i<(1<<dep); i++) f[o][i]=0;
	if (dep==1) {
		for (int i=o/2; i; i/=2) {
			f[o][c[i]] += c[i]==0 ? v[o][i] : u[o][i];
		}
	} else {
		c[o] = 1;
		solve(o*2, dep-1, size, m);
		solve(o*2+1, dep-1, size, m);
		for (int i=0; i<=m && i<(1<<dep); i++) {
			for (int j=0; j<=i && j<(1<<(dep-1)); j++) {
				f[o][i] = max(f[o][i], f[o*2][j]+f[o*2+1][i-j]);
			}
		}
		c[o] = 0;
		solve(o*2, dep-1, size, m);
		solve(o*2+1, dep-1, size, m);
		for (int i=0; i<=m && i<(1<<dep); i++) {
			for (int j=0; j<=i && j<(1<<(dep-1)); j++) {
				f[o][i] = max(f[o][i], f[o*2][j]+f[o*2+1][i-j]);
			}
		}
	}
}

int main() {
#ifdef DEBUG
	freopen("src.in", "r", stdin);
	freopen("src.out", "w", stdout);
#endif

	int n, m, size;
	scanf("%d %d", &n, &m);
	size=1<<n;
	for (int i=1<<(n-1); i<size; i++) {
		for (int j=i/2; j; j/=2) {
			scanf("%d", &u[i][j]);
		}
	}
	for (int i=1<<(n-1); i<size; i++) {
		for (int j=i/2; j; j/=2) {
			scanf("%d", &v[i][j]);
		}
	}

	solve(1, n, size, m);
	int ans=0;
	for (int i=0; i<=m; i++) ans = max(ans, f[1][i]);
	printf("%d\n", ans);

	fclose(stdin);
	fclose(stdout);
	return 0;
}
